package com.dupf.product.web.interceptor;

import com.dupf.upms.rpc.api.UpmsApiService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 后台过滤器
 * Created by dupf on 2018/3/15.
 */
public class ManageInterceptor extends HandlerInterceptorAdapter {

	private static final Logger LOGGER = LoggerFactory.getLogger(ManageInterceptor.class);

	@Autowired
	UpmsApiService upmsApiService;

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

		// 过滤ajax
		if (null != request.getHeader("X-Requested-With") && "XMLHttpRequest".equalsIgnoreCase(request.getHeader("X-Requested-With"))) {
			return true;
		}
//		// 登录信息
//		Subject subject = SecurityUtils.getSubject();
//		String username = (String) subject.getPrincipal();
//
//		JSONObject userInfo = upmsApiService.selectUpmsUserInfoByUsername(username);
//		request.setAttribute("upmsUser", userInfo);
//		request.setAttribute("currentUserId", userInfo.getInt("userId"));
//		request.setAttribute("currentOrgId", userInfo.getInt("organizationId"));
		return true;
	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
	}

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

	}

}
